package com.ag.lzw.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ag.bean.TbUser;
import com.ag.bean.TbOverRequest;
import com.ag.common.AgException;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IDUtil;
import com.ag.common.IIDService;
import com.ag.common.IPermissionService;
import com.ag.lzw.dao.LzwTbEventDAO;
import com.ag.lzw.service.HandelTasks;
import com.ag.lzw.service.IdentifyService;
import com.ag.lzw.vo.TaskDetail;
import com.ag.lzw.vo.TaskOverVo;
import com.ag.wzp.service.impl.DisposalServiceImpl;
import com.ag.zrt.dao.TbProposeDAO;

/**
 * 任务办结的Action
 * 
 * @author 钟龙伟
 */

@Scope("prototype")
@Controller("TaskOverAction")
public class TaskOverAction extends BaseAction implements SessionAware {
	private static final long serialVersionUID = 891781047149206372L;
	/**
	 * 调用权限服务逻辑组件
	 */
	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;
	/**
	 * 提供Map<String, Object>类型的Session变量，用来保存Struts2注入的Session上下文
	 */
	private Map<String, Object> sessionMap;

	@Resource(name = "handelTaskImpl")
	private HandelTasks handelTask;

	@Resource(name = "iDServiceImpl")
	private IIDService idservice;

	@Resource(name = "lzwTbEventDAO")
	private LzwTbEventDAO tbEventDAO;

	@Resource(name = "ZRTTbProposeDAO")
	private TbProposeDAO proposeDAO;
	
	@Resource(name = "IdentifyServiceImpl")
	private IdentifyService identifyServ;

	private Map<String, String> queryOption; // 接收页面传过来的参数
	private String taskid;
	private String tid;
	private String toid;
	private String message;
	private int index;
	private TbOverRequest tboverRequest;
	private String rightString;
	private String cuname;
	private String name;
	private String tcontext;
	private String leader;
	
	TaskDetail tdl = new TaskDetail();

	public String overRight() { // 权限判断是否能进行办结

		TbUser user = pms.getCurrentUser(sessionMap); // 获取当前用户信息
		String cUname = user.getUName();
		this.setCuname(cUname);

		System.out.println("++++++++" + toid);

		if (!toid.equals("noResult")) {
			tboverRequest = handelTask.findTboverRequestByToId(toid).iterator()
					.next();
			System.out.println("+++++=====11========+++");
			if (handelTask.queryTaskById(handelTask.queryTaskById(taskid)
					.getFid()) != null
					&& cUname.equals(handelTask.queryTaskById(
							handelTask.queryTaskById(taskid).getFid())
							.getUname())) { // 当前用户与该任务的父任务的用户进行比较
				System.out.println("+++++===22==========+++");
				if ((tboverRequest.getTbTask().getTbTaskStatus().getTsName()
						.equals("已办结") && tboverRequest.getTbOverStatus()
						.getSname().equals("办结申请"))) {
					this.setRightString("read_task_over_request"); // 未处理过，只能看到申请的项目
					// return "has_task_over_right";
				} else if (tboverRequest.getTbOverStatus().getSname().equals(
						"办结回退")) {
					this.setRightString("Overback");
					// return "goto_the_task_has_over";
				} else if (tboverRequest.getTbTask().getTbTaskStatus()
						.getTsName().equals("已办结")) {
					this.setRightString("Oversuccess");
					// return "goto_the_task_has_over";
				} else {
					this.setRightString("ok"); // 有办结权限
					// return "has_task_over_right"; //可以办结
				}

			} else if (cUname.equals(handelTask.queryTaskById(taskid)
					.getUname())) {
				System.out.println("+++++======33=======+++");
				if (tboverRequest.getTbOverStatus().getSname().equals("办结回退")) {
					this.setRightString("Overback_then_apply");
					// return "goto_the_task_has_over";
				} else if ((tboverRequest.getTbTask().getTbTaskStatus()
						.getTsName().equals("已办结") && tboverRequest
						.getTbOverStatus().getSname().equals("办结申请"))) {
					this.setRightString("read_task_over_request"); // 未处理过，只能看到申请的项目
					// return "has_task_over_right";
				} else if (tboverRequest.getTbTask().getTbTaskStatus()
						.getTsName().equals("已办结")) {
					this.setRightString("Oversuccess");
					// return "goto_the_task_has_over";
				} else if (tboverRequest.getTbOverStatus().getSname().equals(
						"办结收回")
						&& !tboverRequest.getTbTask().getTbTaskStatus()
								.getTsName().equals("办结申请")) {
					tid = handelTask.queryTaskById(taskid).getId();
					name = handelTask.queryTaskById(taskid).getName();
					tcontext = handelTask.queryTaskById(taskid).getContext();
					this.setRightString("apply_task_over"); // 可申请
					// return "has_task_over_right";
				} else if (tboverRequest.getTbOverStatus().getSname().equals(
						"办结收回")) {
					this.setRightString("read_task_over_request"); // 收回的申请，只能看到申请的项目
					// return "has_task_over_right";
				} else { // 未处理
					this.setRightString("has_no_right"); // 可以收回申请
					// return "has_task_over_right"; // 无办结权限
				}
			}
			// } else {
			// this.setRightString("该任务没有父任务！");
			// return "error";
			// }
		} else {
			System.out.println("==========" + taskid);
			if (handelTask.queryTaskById(taskid).getStatus().equals("已办结")) {
				this.setRightString("has_over"); // 已办结的任务不能提出申请
			} else {
				if (handelTask.queryTaskById(taskid) != null) {
					tid = handelTask.queryTaskById(taskid).getId();
					name = handelTask.queryTaskById(taskid).getName();
					tcontext = handelTask.queryTaskById(taskid).getContext();
					String tuname = handelTask.queryTaskById(taskid).getUname();
					System.out.println("++++========tName+++++++" + tuname);
					if (tuname.equals(cUname)) {
						this.setRightString("apply_task_over");
					} else {
						this.setRightString("have_no_right_apply_task_over");
					}
				}
			}
		}
		return "has_task_over_right";
	}

	public String request() { // 提交办结申请
		try {
			TaskOverVo tover = new TaskOverVo();
			// Date nowTime = (Date) new java.util.Date();
			// System.out.println("++++++++++++++++time="+nowTime.toString());
			tover.setToid(idservice.findTablePK(IDUtil.TB_OVER_REQUEST));
			System.out.println("++++++++++++++++toid="
					+ idservice.findTablePK(IDUtil.TB_OVER_REQUEST));
			tover.setTaskid(queryOption.get("taskid").toString());
			// tover.setToTime(nowTime.toString());
			tover.setToReason(queryOption.get("toReason").toString());
			tover.setApplyPerson(queryOption.get("applyPerson").toString());
			if (handelTask.saveOverRequest(tover)) {
				this.setMessage("申请提交成功！");
			} else
				this.setMessage("申请提交失败！");
		} catch (AgException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "task_request_succ";
	}

	public String over() { // 任务办结
		TaskOverVo tover = new TaskOverVo();
		// Date nowTime = (Date) new java.util.Date();
		tover.setToid(queryOption.get("toid").toString());
		tover.setTaskid(queryOption.get("taskid").toString());
		// System.out.println("++++++++++++++++time="+nowTime.toString());
		// tover.setToTime(nowTime.toString());
		tover.setToReason(queryOption.get("toReason").toString());
		tover.setThandeled(queryOption.get("advice").toString());
		tover.settPerson(queryOption.get("tPerson").toString()); // 办结人
		tover.setApplyPerson(queryOption.get("applyPerson").toString()); // 办结申请人
		if (handelTask.saveOverTask(tover)) {
			this.setMessage("办结提交成功！");
		} else
			this.setMessage("办结提交失败！");
		return "task_over_succ";
	}

	public String disagree() { // 任务退办
		TaskOverVo tover = new TaskOverVo();
		// Date nowTime = (Date) new java.util.Date();
		tover.setToid(queryOption.get("toid").toString());
		tover.setTaskid(queryOption.get("taskid").toString());
		// tover.setToTime(nowTime.toString());
		tover.setToReason(queryOption.get("toReason").toString());
		tover.setToFailue(queryOption.get("advice").toString());
		tover.settPerson(queryOption.get("tPerson").toString()); // 办结人
		tover.setApplyPerson(queryOption.get("applyPerson").toString()); // 办结申请人
		if (handelTask.saveOverFail(tover)) {
			this.setMessage("退办提交成功！");
		} else
			this.setMessage("退办提交失败！");
		return "task_over_disagree";
	}

	public String apply() { // 申请任务办结
		System.out.println("==========" + tid);
		if (handelTask.queryTaskById(tid).getStatus().equals("已办结")) {
			return "the_task_has_over"; // 已办结的任务不能提出申请
		} else {
			TbUser user = pms.getCurrentUser(sessionMap); // 获取当前用户信息
			String cUname = user.getUName();
			this.setCuname(cUname);
			if (handelTask.queryTaskById(tid) != null) {
				taskid = handelTask.queryTaskById(tid).getId();
				name = handelTask.queryTaskById(tid).getName();
				System.out.println("++++========tName+++++++" + name);
				tcontext = handelTask.queryTaskById(tid).getContext();
				return "apply_task_over";
			} else {
				return "error";
			}
		}
	}
	
	public String isLeader() {             //判断该用户是否为指挥长
		TbUser currentUser = this.pms.getCurrentUser(sessionMap);
		String uid = currentUser.getUId(); // 获取当前用户ID
		System.out.println("++++++++++++++++uid++=========="+uid);
		boolean isLeader = false;
		System.out.println("++++++++++++++++tid++=========="+tid);
		String eid = handelTask.queryTaskById(tid).getEId();
		try {
			// 调用service分辨当前用户是否指挥长
			isLeader = identifyServ.isLeader(uid, eid);
		} catch (Exception e) {
		}
		if (isLeader == true) {
			this.setLeader("1");
		} else {
			this.setLeader("2");
		}
		return "isLeader";
	}

	public String directToApply() { // 获取指挥长办结任务前的一些信息
		System.out.println("==========" + toid);
		System.out.println("==========" + taskid);

		TbUser user = pms.getCurrentUser(sessionMap); // 获取当前用户信息
		String cUname = user.getUName();
		String uid = user.getUId();
		System.out.println("===========cUname============" + cUname);
		System.out.println("===========uid============" + uid);
		this.setCuname(cUname);

		// String masterId = this.tbEventDAO.findById(
		// handelTask.queryTaskById(taskid).getEId()).getTbUserByTbUId()
		// .getUId();
		try {
			System.out.println("===========eid============" + handelTask.queryTaskById(taskid).getEId());
			String masterName = this.tbEventDAO.findAEventById(handelTask.queryTaskById(taskid).getEId()).getTbUserByTbUId().getUName();   //出错！懒加载
			
			System.out.println("===========masterName============" + masterName);
//				this.proposeDAO.findProposeByEventId(
//					(String) getSession().getAttribute(
//							EventUtil.PROCESSING_EVENT_ID))
//					.getTbUserByPViceCommander().getUName();
			System.out
					.println("===========getLeadname============" + handelTask.queryTaskById(
							taskid).getLeadname());
			System.out.println("===========tuName============"
					+ handelTask.queryTaskById(taskid).getUname());

			if (toid.equals("noResult")) {
				if (handelTask.queryTaskById(taskid).getStatus().equals("已办结")) {
					this.setRightString("has_over"); // 已办结的任务不能提出申请
				} else {
					if (handelTask.queryTaskById(taskid) != null
							&& masterName.equals(handelTask.queryTaskById(
									taskid).getLeadname())) {
						tid = handelTask.queryTaskById(taskid).getId();
						name = handelTask.queryTaskById(taskid).getName();
						System.out.println("++++========tName+++++++" + name);
						tcontext = handelTask.queryTaskById(taskid)
								.getContext();
						this.setRightString("dircetor_to_over");
					} else {
						this.setRightString("noRecord");
					}
				}
			} else {
				tboverRequest = handelTask.findTboverRequestByToId(toid)
						.iterator().next();
				System.out.println("+++++=====11========+++");
				System.out.println("+++++=====getLeadname========+++"
						+ handelTask.queryTaskById(taskid).getLeadname());
				if (!handelTask.queryTaskById(taskid).getStatus()
								.equals("已办结")
						&& tboverRequest.getTbOverStatus().getSname().equals(
								"办结回退")) {
					this.setRightString("show_the_record");
				} else {
					if (tboverRequest.getTbTask().getTbTaskStatus().getTsName()
							.equals("已办结")
							&& tboverRequest.getTbOverStatus().getSname()
									.equals("办结申请")) {
						this.setRightString("read_task_over_request"); // 未处理过，只能看到申请的项目
						// return "has_task_over_right";
					} else if (tboverRequest.getTbOverStatus().getSname()
							.equals("办结回退")) {
						System.out.println("+++++=====ffffffffff========+++");
						this.setRightString("Overback"); // 办结回退的，只能看
						// return "goto_the_task_has_over";
					} else if (tboverRequest.getTbTask().getTbTaskStatus()
							.getTsName().equals("已办结")) {
						this.setRightString("Oversuccess");
						// return "goto_the_task_has_over";
					} else {
						System.out.println("+++++=====44444444========+++");
						this.setRightString("ok"); // 有办结权限
						// return "has_task_over_right"; //可以办结
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "has_task_over_right";
	}

	public String directOverRight() { // 指挥长查看办结详情或处理下级的办结申请
		TbUser user = pms.getCurrentUser(sessionMap); // 获取当前用户信息
		String cUname = user.getUName();
		this.setCuname(cUname);

		tboverRequest = handelTask.findTboverRequestByToId(toid).iterator()
				.next();

		if (cUname.equals(handelTask.queryTaskById(taskid).getUname())) {
			return "direct_goto_task_over";
		} else {
			if ((tboverRequest.getTbTask().getTbTaskStatus().getTsName()
					.equals("已办结") && tboverRequest.getTbOverStatus()
					.getSname().equals("办结申请"))) {
				this.setRightString("read_task_over_request"); // 未处理过，只能看到申请的项目
				return "direct_has_task_over_right";
			} else if (tboverRequest.getTbOverStatus().getSname()
					.equals("办结回退")) {
				this.setRightString("Overback");
				return "direct_goto_the_task_has_over";
			} else if (tboverRequest.getTbTask().getTbTaskStatus().getTsName()
					.equals("已办结")) {
				this.setRightString("Oversuccess");
				return "direct_goto_the_task_has_over";
			} else {
				this.setRightString("ok"); // 有办结权限
				return "direct_has_task_over_right"; // 可以办结
			}
		}
	}

	public String directToOver() { // 指挥长办结自己的任务
		TaskOverVo tover = new TaskOverVo();
		tover.setToid(idservice.findTablePK(IDUtil.TB_OVER_REQUEST));
		tover.setTaskid(queryOption.get("taskid").toString());
		// System.out.println("======toTime========="
		// + queryOption.get("toTime").toString());
		// tover.setToTime(queryOption.get("toTime").toString());
		tover.setThandeled(queryOption.get("advice").toString());
		tover.settPerson(queryOption.get("tPerson").toString()); // 办结人

		// tover.setToReason(null);
		// tover.setApplyPerson(null);
		if (handelTask.directSaveOverTask(tover)) {
			this.setMessage("办结提交成功！");
		} else
			this.setMessage("办结提交失败！");
		return "direct_task_over_succ";
	}

	public String getBack() { // 收回申请
		TaskOverVo tover = new TaskOverVo();
		tover.setToid(queryOption.get("toid").toString());
		tover.setTaskid(queryOption.get("taskid").toString());
		tover.setToReason(queryOption.get("toReason").toString());
		tover.setApplyPerson(queryOption.get("applyPerson").toString());

		if (handelTask.getBackTaskOverRequest(tover)) {
			this.setMessage("收回成功！");
		} else
			this.setMessage("收回失败！");
		return "get_back_task_over_request";
	}

	public Map<String, String> getQueryOption() {
		return queryOption;
	}

	public void setQueryOption(Map<String, String> queryOption) {
		this.queryOption = queryOption;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String getTaskid() {
		return taskid;
	}

	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.sessionMap = session;

	}

	public String getToid() {
		return toid;
	}

	public void setToid(String toid) {
		this.toid = toid;
	}

	public TbOverRequest getTboverRequest() {
		return tboverRequest;
	}

	public void setTboverRequest(TbOverRequest tboverRequest) {
		this.tboverRequest = tboverRequest;
	}

	public String getRightString() {
		return rightString;
	}

	public void setRightString(String rightString) {
		this.rightString = rightString;
	}

	public String getCuname() {
		return cuname;
	}

	public void setCuname(String cuname) {
		this.cuname = cuname;
	}

	public TaskDetail getTdl() {
		return tdl;
	}

	public void setTdl(TaskDetail tdl) {
		this.tdl = tdl;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTcontext() {
		return tcontext;
	}

	public void setTcontext(String tcontext) {
		this.tcontext = tcontext;
	}

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public String getLeader() {
		return leader;
	}

	public void setLeader(String leader) {
		this.leader = leader;
	}

}
